package com.zs.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.blog.converter.CategoryConverter;
import com.zs.blog.converter.RoleConverter;
import com.zs.blog.dao.IRoleMapper;
import com.zs.blog.exception.BusinessException;
import com.zs.blog.model.dto.CategoryDTO;
import com.zs.blog.model.dto.RoleEditDTO;
import com.zs.blog.model.dto.RoleEditStatusDTO;
import com.zs.blog.model.dto.RoleSearchDTO;
import com.zs.blog.model.entity.Category;
import com.zs.blog.model.entity.Role;
import com.zs.blog.model.vo.CategoryListVO;
import com.zs.blog.model.vo.PageInfoVO;
import com.zs.blog.model.vo.RoleListVO;
import com.zs.blog.model.vo.UserListVO;
import com.zs.blog.service.IRoleService;
import com.zs.blog.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Title: IRoleServiceImpl
 * @Author zs
 * @Package com.zs.blog.service.impl
 * @Date 2025/5/18 17:59
 * @description: 角色业务逻辑实现类
 */
@Service
public class IRoleServiceImpl extends ServiceImpl<IRoleMapper, Role> implements IRoleService {

    public IRoleMapper roleMapper;

    public IRoleServiceImpl(IRoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    /**
     * 获取分页的角色列表信息
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @return 分页的角色列表信息
     */
    @Override
    public PageInfoVO<RoleListVO> listRoleWithPagination(Integer pageNum, Integer pageSize) {
        // 分页对象
        Page<Role> page = new Page<>(pageNum, pageSize);

        // 构建查询条件
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getIsDeleted, 0);
        // 查询数据
        List<Role> roles = roleMapper.selectList(page, lqw);

        // 属性映射
        List<RoleListVO> roleListVOS = RoleConverter.INSTANCE.RoleList2RoleListVO(roles);

        // 组装数据并返回
        PageInfoVO<RoleListVO> pageInfoVO = new PageInfoVO<>();
        pageInfoVO.setTotal(page.getTotal());
        pageInfoVO.setItems(roleListVOS);
        return pageInfoVO;
    }

    /**
     * 新增角色
     *
     * @param roleEditDTO 新增的角色信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRole(RoleEditDTO roleEditDTO) {
        // 判断是否有相同的角色信息
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getRoleCode, roleEditDTO.getRoleCode());
        Role role = roleMapper.selectOne(lqw);

        if (Objects.nonNull(role)) {
            throw new BusinessException("角色已存在");
        }

        Role insertRole = RoleConverter.INSTANCE.roleEditDTO2Role(roleEditDTO);
        roleMapper.insertOrUpdate(insertRole);
    }

    /**
     * 更新角色
     *
     * @param roleEditDTO 新增的角色信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(RoleEditDTO roleEditDTO) {
        // 判断是否有要修改的的角色信息
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getRoleCode, roleEditDTO.getRoleCode());
        Role role = roleMapper.selectOne(lqw);

        if (Objects.isNull(role)) {
            throw new BusinessException("角色不存在");
        }

        Role updateRole = RoleConverter.INSTANCE.roleEditDTO2Role(roleEditDTO);
        roleMapper.insertOrUpdate(updateRole);
    }

    /**
     * 更新角色禁用启用状态
     *
     * @param roleEditStatusDTO 要修改角色的状态信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleStatus(RoleEditStatusDTO roleEditStatusDTO) {
        // 判断是否有要修改的的角色信息
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getRoleCode, roleEditStatusDTO.getRoleId());
        Role role = roleMapper.selectOne(lqw);

        if (Objects.isNull(role)) {
            throw new BusinessException("角色不存在");
        }

        Role updateRole = RoleConverter.INSTANCE.roleEditStatusDTO2Role(roleEditStatusDTO);
        roleMapper.insertOrUpdate(updateRole);
    }

    /**
     * 根据搜索条件查询分页的角色列表
     * @param roleSearchDTO 角色搜索条件实体类
     * @return 分页的角色列表
     */
    @Override
    public PageInfoVO<RoleListVO> listRoleBySearchCondition(RoleSearchDTO roleSearchDTO) {
        Page<Role> page = new Page<>(roleSearchDTO.getPageNum(), roleSearchDTO.getPageSize());

        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(roleSearchDTO.getRoleName()), Role::getRoleName, roleSearchDTO.getRoleName())
                .eq(roleSearchDTO.getIsDeleted() != null , Role::getIsDeleted, roleSearchDTO.getIsDeleted());
        if (roleSearchDTO.getStartTime() != null && roleSearchDTO.getEndTime() != null) {
            lqw.between(Role::getCreateTime, roleSearchDTO.getStartTime(), roleSearchDTO.getEndTime());
        }

        List<Role> roleList = roleMapper.selectList(page, lqw);

        List<RoleListVO> roleListVOS = RoleConverter.INSTANCE.RoleList2RoleListVO(roleList);

        PageInfoVO<RoleListVO> pageInfoVO = new PageInfoVO<>();
        pageInfoVO.setTotal(page.getTotal());
        pageInfoVO.setItems(roleListVOS);
        return pageInfoVO;
    }


}
